using System;

namespace Scenario.Modules.DM.DataModel
{
	public class XMLDocumentVersions : bOSdotNET.XML.Data.XMLObjectSet 
	{
		public const string MAIN_NODE	= "Versions";

		public XMLDocumentVersions()
		{
			this.Init();
		}

		public XMLDocumentVersions(System.Xml.XmlNode nodeList)
		{
			this.Parse (nodeList);
		}

		public override void Init()
		{
			this.RemoveAll();
		}

		public override void Parse(System.Xml.XmlNode nodeList)
		{
			this.Init();

			System.Xml.XmlNode node;

			for ( int i=0; i<nodeList.ChildNodes.Count;i++)
			{
				node = nodeList.ChildNodes[i];

				switch ( node.Name) 
				{
					case XMLDocumentVersion.MAIN_NODE:
						this.Add ( new XMLDocumentVersion(node));
						break;
				}
			}
		}

		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLDocumentVersions.MAIN_NODE);

			for ( int i=0; i<this.Count;i++)
			{
				xmlNode.AddNode ( this[i].GetXMLNode() );
			}

			return xmlNode;
		}

		public XMLDocumentVersion Get (string sVersionId)
		{
			XMLDocumentVersion version;

			for ( int i=0; i<this.List.Count;i++)
			{
				version = (XMLDocumentVersion)this[i];

				if ( version.Id == sVersionId )
				{
					return version;
				}
			}

            throw new Scenario.Modules.DM.DataModel.Exception.VersionNotFound(sVersionId);
		}

		public XMLDocumentVersion GetLastVersion()
		{
			if ( this.List.Count > 0 )
			{
				return (XMLDocumentVersion)this.List[this.List.Count-1];
			}
			else
			{
				return null;
			}
		}

		public XMLDocumentVersion AddVersion(XMLDocumentVersion version)
		{
			XMLDocumentVersion version2Add = version;
			int idVersion = this.List.Count + 1;
			version2Add.Id = idVersion.ToString();

			this.List.Add (version2Add);

			return version2Add;
		}

		public XMLDocumentVersion AddVersion()
		{
			XMLDocumentVersion version = new XMLDocumentVersion();
			this.AddVersion (version);
			return version;
		}

	}
}